﻿<extend name="Public/content" />
<block name="style">
<style type="text/css">
    .table>tbody>tr>td{ padding: 4px; vertical-align: middle;}
        body{ background: #fff;}
   .footer {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 0;
   
    left: 0;
    line-height: 50px;
    text-align: right;
    text-align: right;
    border-top: solid 1px #eee;
    padding-right: 10px;
    z-index: 999999999999999;
}
</style>
</block>
   <block name="body">
    
        <input type="hidden" name="delivery_id" value="{$delivery_id}" id="delivery_id">
    <include file="Public:artdialog" />
    <div class="row">
        <div class="col-md-12">
            <div class="block-web">
                <div class="header">
                    <h3 class="content-header">确定预订单采购列表</if></h3>
                </div>

                <div class="porlets-content">

                    <div class="table-responsive">
               <div class="form-group" >
                        <div class="input-group col-sm-3"  style="margin-bottom:10px">
                         <input class="form-control" id="title" type="text" name="title" placeholder="请输入该采购标题">

                     </div>
                     <span style="color:red;font-size:20px; float:right">*确保信息填写跟线下沟通一致</span>
                 </div>
                        <div id="order_list_item_content">
                                <table id="sample-table-2" class="table table-striped table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>序号</th>
                                            <th>商品单号</th>
                                            <th>价格</th>
                                            <th>数量</th>
                                            <th>有效期</th>
                                            <th>预估交货时间</th>
                                            <th>操作</th>
                                        </tr>
                                    </thead>
                                    <tbody id="lists_order">
                         
                                    </tbody>
                                </table>
                        </div>
                    </div><!--/table-responsive-->
                </div><!--/porlets-content-->
            </div><!--/block-web-->
        </div><!--/col-md-12-->
    </div>
    <div class="footer" >
        <button class="btn btn-info " type="button" onclick="sub_order_list_all()" >确定提交</button>
        <button class="btn btn-default bt_close" type="button" onclick="del_order_list_all()" >取消关闭</button>
    </div>
</block>

<block name="script">

<script type="text/javascript">
 var cookiename2="pre_order_<?php echo UID; ?>";
    $(function(){
        //初始化
        load();

    });

 function load(){
   var ojson=[],lsjson=[],json=[],del_json=[]; 
   var htl="";
    //获取cookie值 
    var _list=getCookie(cookiename2);
    if(_list!=""){
      ojson=JSON.parse(_list);
    }
    var htm = "";
    if(ojson == null){
        _hide_public_loading();
        return;
    }
   
    var data_arr ={'1':'一个月','3':'三个月','6':'半年','12':'一年','24':'两年'}
    for(var i=0;i<ojson.length;i++){
        var date= getLocalTime(ojson[i].Delivery).split(' ');
        var d = ojson[i].Validity;
        htm +='<tr><td>'+(i+1)+'</td><td>'+ojson[i].GoodsSn+'</td><td>'+ojson[i].price+'</td><td>'+ojson[i].Quantity+'</td><td>'+data_arr[d]+'</td><td>'+date[0]+'</td><td><button type="button" class="btn btn-info btn-sm" field=\'"GoodsSn":"'+ojson[i].GoodsSn+'","price":"'+ojson[i].price+'","Quantity":"'+ojson[i].Quantity+'","Delivery":"'+ojson[i].Delivery+'","Validity":"'+ojson[i].Validity+'"\' onclick="del_order_list(this)"> 删除</button></td></tr>';
    }
    $('#lists_order').append(htm);
    _hide_public_loading();
 }




  //时间戳转日期
    function getLocalTime(nS) {     
       return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-");      
    }   
  

 //删除cookie中的数据
 function del_order_list(_this){
     var ojson=[],lsjson=[],json=[],del_json=[];
     var _json = $(_this).attr('field');
     _json="{"+_json+"}";
     var tmp =JSON.parse(_json);
    //删除html
    $(_this).parent('td').parent('tr').remove();
     //获取cookie
    var _list=getCookie(cookiename2);
    if(_list!=""){
      ojson=JSON.parse(_list);
    }
    for(var i=0;i<ojson.length;i++){
        if(ojson[i].GoodsSn == tmp['GoodsSn']){
            del_json.push(tmp);  //删除的
        }else{
            lsjson.push(ojson[i]); //剩余的
        }
    }
    if(lsjson.length>0){
        setCookie(cookiename2,"",-10);
        var _db=JSON.stringify(lsjson);
        setCookie(cookiename2,_db,1);
         showvfmsg(1, '删除成功', 2000);return;
    }else{
        if(del_json.length>0){
            setCookie(cookiename2,"",-10);
            showvfmsg(1, '删除成功', 2000);return;
        }
    }

 }

 //提交该采购单
 function sub_order_list_all(){
   
    var lsjson =[];
   //获取到该采购单标题
   var title = $('#title').val();
   if(title ==""){
     showvfmsg(0, '请填写采购单标题', 2000);return;
   }
   //获取到采购单内容
   $("#lists_order").children('tr').each(function(){
        var _json=$(this).children('td').eq(6).find('button').attr("field");
        var _json="{"+_json+"}";
        var tmp =JSON.parse(_json);
        if(tmp != ""){
           lsjson.push(tmp);  //当前特价商品信息
        }
   });
  if(lsjson ==""){
     showvfmsg(0, '没有要采购的商品', 2000);return;
   }

    var GoodsJsonStr =JSON.stringify(lsjson);
    _show_public_loading();
   //提交采购单
         $.ajax({
             'url': "{:U('PreOrder/sub_purchase_order')}",
             'data': {'title':title,'GoodsJsonStr':GoodsJsonStr},
             // 'dataType': 'json',
             'type': 'POST',
             success: function (data) {
               showvfmsg(data.state, data.msg, 2000);
               $('#money',window.parent.document).html(data.money);
                 if (data.state =="1") {  
                    setTimeout(function(){ 
                      del_order_list_all();
                    },2000);
                 }
             }, error: function (err) { 
                 console.log(err);
             },complete: function () {
                  _hide_public_loading();
             }
         });

 }
 //删除cookie中的数据
 function del_order_list_all(){
     setCookie(cookiename2,"",-10);
     art.dialog.open.api.close();
 }

//写入cookie
 function setCookie(c_name, value, expiredays){
    if(expiredays < 0){
        localStorage.removeItem(c_name);
    }
       localStorage.setItem(c_name,value); //存储名字为name值为caibin的变量
 }
//读取cookie   
 function getCookie(c_name){
    return  localStorage.getItem(c_name);
 }

function clock_load_dialog(){
   setTimeout(function(){
    var win=    art.dialog.opener;
                win.load();
                top.art.dialog().close();
    },800);
}




</script>
</block>
